﻿Public Class frmCJAdd
    Private Sub frmCJAdd_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim icount As Integer
        Dim i As Integer
        cboZY.Items.Clear()
        cboCourse.Items.Clear()
        cboStudent.Items.Clear()
        txtSQL = "SELECT 专业名称 FROM 专业 "
        icount = ExecuteSQL(txtSQL, ErrorMsg)
        For i = 0 To icount - 1
            cboZY.Items.Add(DBSet.Tables.Item(0).Rows(i).Item("专业名称").ToString)
        Next i
        '
        txtSQL = "SELECT 课程编号,课程名称 FROM 课程 "
        icount = ExecuteSQL(txtSQL, ErrorMsg)
        For i = 0 To icount - 1
            cboCourse.Items.Add(Trim(DBSet.Tables.Item(0).Rows(i).Item("课程编号").ToString) & "--" & DBSet.Tables.Item(0).Rows(i).Item("课程名称").ToString)
        Next i
        cboCourse.SelectedIndex = -1
    End Sub

    Private Sub cboZY_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboZY.SelectedIndexChanged
        Dim icount As Integer
        Dim i As Integer
        cboStudent.Items.Clear()
        If cboZY.Text = "" Then
            MsgBox("您还没有选择专业!")
            Exit Sub
        End If
        txtSQL = "SELECT 学号,姓名 FROM 学生 " & " WHERE 专业名称='" & cboZY.Text & "'"
        icount = ExecuteSQL(txtSQL, ErrorMsg)
        For i = 0 To icount - 1
            cboStudent.Items.Add(Trim(DBSet.Tables.Item(0).Rows(i).Item("学号").ToString) & "--" & DBSet.Tables.Item(0).Rows(i).Item("姓名").ToString)
        Next i
        cboStudent.SelectedIndex = -1
    End Sub
    '单击"添加"按钮
    Private Sub cmdAdd_Click(sender As Object, e As EventArgs) Handles cmdAdd.Click
        Dim xh As String
        Dim kz As String
        Dim icount As Integer
        If cboCourse.Text = " " Then
            MsgBox("您还没有选择课程!")
            Exit Sub
        End If
        If cboStudent.Text = " " Then
            MsgBox("您还没有选择学生学号!")
            Exit Sub
        End If
        '
        If Not IsNumeric(txtScore.Text) Then
            MsgBox("分数输入不为数字!")
            txtScore.Focus()
            Exit Sub
        End If
        '
        kz = Microsoft.VisualBasic.Left(cboCourse.Text, InStr(1, cboCourse.Text, "-") - 1)
        xh = Microsoft.VisualBasic.Left(cboStudent.Text, InStr(1, cboStudent.Text, "-") - 1)
        txtSQL = "SELECT * FROM 选课 WHERE 学号='" & xh & "' AND 课程编号='" & kz & "'"
        icount = ExecuteSQL(txtSQL, ErrorMsg)
        If icount <> 0 Then
            MsgBox("这个学生的这门课成绩已录过!", vbOKOnly, "警告")
            Exit Sub
        Else
            txtSQL = "INSERT INTO 选课(学号,课程编号,成绩) VALUES ('" & xh & "','" & kz & "'," & txtScore.Text & ")"
            icount = ExecuteSQL(txtSQL, ErrorMsg)
            MsgBox("写入成功!", vbOKOnly)
            txtScore.Text = ""
            txtScore.Focus()
        End If
    End Sub

    Private Sub cmdExit_Click(sender As Object, e As EventArgs) Handles cmdExit.Click
        Me.Close()
    End Sub
End Class